package com.cn.yang.dao.impl;

import com.cn.yang.dao.UserDao;
import com.cn.yang.model.UserModel;
import com.cn.yang.util.GetSqlSession;
import com.cn.yang.util.JDBC;
import com.cn.yang.util.PageUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

//新增用户
    @Override
    public int registerUser(UserModel userModel) {
        UserDaoImpl registerDao = new UserDaoImpl();
        int a = -1;
        try{
            if (!registerDao.selectbyUserName(userModel)){
                a = GetSqlSession.getSqlSession().insert("com.cn.yang.dao.UserDao.registerUser",userModel);
                System.out.println(a);
                return a;
            }else{
                return a;
            }
        }catch (Exception e){
            e.getMessage();
        }
        return a;
    }
//查询用户是否有值
    @Override
    public boolean selectbyUserName(UserModel userModel) {
        boolean a = false;
        try {
            a = GetSqlSession.getSqlSession().selectOne("com.cn.yang.dao.UserDao.selectbyUserName",userModel);
        }catch (Exception e){
            e.getMessage();
        }
        
        System.out.println(a);
        return a;
    }
//登录
    @Override
    public int login(UserModel userModel) {
        UserDaoImpl registerDao = new UserDaoImpl();
        registerDao.selectbyUserName(userModel);
        if (!registerDao.selectbyUserName(userModel)){
            return -1;
        }else {
            int a = 0;
            try{
                if (GetSqlSession.getSqlSession().selectOne("com.cn.yang.dao.UserDao.login",userModel)==null?false:true){
                    a=1;
                }else {
                    a=2;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return a;
        }
    }
//查询所有数据
//     @Override
//     public List<UserModel> findAllUser(UserModel userModel) {
//         return GetSqlSession.getSqlSession().selectList("com.cn.yang.dao.UserDao.findAllUser");
//     }
    //删除一条数据
    @Override
    public int deleteById(Integer id) {
        int result = GetSqlSession.getSqlSession().delete("com.cn.yang.dao.UserDao.deleteById",id);
        if (result==1){
            return 1;
        }else {
            return 0;
        }
    }
//查询一条数据
    @Override
    public UserModel findIdUser(Integer id) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.yang.dao.UserDao.findIdUser",id);
    }
//更新数据库  用户数据============================
    @Override
    public int updateUser(UserModel userModel) {
        //成功为1， 失败为0
        return GetSqlSession.getSqlSession().update("com.cn.yang.dao.UserDao.updateUser",userModel);
    }
//模糊查询
    @Override
    public List<UserModel> findSomeUser(UserModel userModel) {
        return GetSqlSession.getSqlSession().selectList("com.cn.yang.dao.UserDao.findSomeUser",userModel);
    }
//总条数
    @Override
    public int countUser(UserModel userModel) {
        int a = GetSqlSession.getSqlSession().selectOne("com.cn.yang.dao.UserDao.countUser",userModel);
        System.out.println(a);
        return a;
    }
}
